﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GSdk.Net.Lglcd
{
    /// <summary>
    /// Used to see buttons state, it supports sorting (you can put states in a sorted list)
    /// </summary>
    public class DeviceState : IComparable, IComparable<DeviceState>
    {
        #region Comparing operations
        public int CompareTo(object obj)
        {
            return GetHashCode().CompareTo(obj.GetHashCode());
        }

        public int CompareTo(DeviceState other)
        {
            return CompareTo(other as object);
        }

        public override bool Equals(object obj)
        {
            return CompareTo(obj) == 0;
        }

        public static bool operator ==(DeviceState a, DeviceState b)
        {
            return a.Equals(b);
        }

        public static bool operator !=(DeviceState a, DeviceState b)
        {
            return !(a == b);
        }

        public override int GetHashCode()
        {
            return m_PacketNumber;
        }
        #endregion

        private int m_PacketNumber;

        #region ctor
        public DeviceState()
            : this(0U)
        {
        }

        public DeviceState(int buttons)
        {
            m_PacketNumber = buttons;
        }

        public DeviceState(uint buttons)
        {
            m_PacketNumber = (int)buttons;
        }

        public DeviceState(DevicesButtons buttons)
        {
            m_PacketNumber = (int)((uint)buttons);
        }
        #endregion

        public int PacketNumber { get { return m_PacketNumber; } }


        public DevicesButtons Buttons { get { return (DevicesButtons)(uint)m_PacketNumber; } }

        public bool IsButtonDown(DevicesButtons button) { return ((DevicesButtons)(uint)m_PacketNumber).HasFlag(button); }
        public bool IsButtonUp(DevicesButtons button) { return !IsButtonDown(button); }

        public bool Up { get { return IsButtonDown(DevicesButtons.Up); } }
        public bool Down { get { return IsButtonDown(DevicesButtons.Down); } }
        public bool Left { get { return IsButtonDown(DevicesButtons.Left); } }
        public bool Right { get { return IsButtonDown(DevicesButtons.Right); } }
        public bool Ok { get { return IsButtonDown(DevicesButtons.Ok); } }
        public bool Cancel { get { return IsButtonDown(DevicesButtons.Cancel); } }
        public bool Menu { get { return IsButtonDown(DevicesButtons.Menu); } }

        public bool Button0 { get { return IsButtonDown(DevicesButtons.Button0); } }
        public bool Button1 { get { return IsButtonDown(DevicesButtons.Button1); } }
        public bool Button2 { get { return IsButtonDown(DevicesButtons.Button2); } }
        public bool Button3 { get { return IsButtonDown(DevicesButtons.Button3); } }
        public bool Button4 { get { return IsButtonDown(DevicesButtons.Button4); } }
        public bool Button5 { get { return IsButtonDown(DevicesButtons.Button5); } }
        public bool Button6 { get { return IsButtonDown(DevicesButtons.Button6); } }
        public bool Button7 { get { return IsButtonDown(DevicesButtons.Button7); } }

        public override string ToString()
        {
            return Buttons.ToString();
        }
    }
}
